package com.artollkitplus.benchmark;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.artollkitplus.benchmark.R;

import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Environment;
import android.util.Log;

public class IO {
	
	static String sdCardDir = Environment.getExternalStorageDirectory().toString();
	
	public static boolean isSdcardAvailable(){
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			return true;
		}else{
			return false;
		}
		
		
	}
	
	/**
	 * transfers required files to the sc card to be access from C Code
	 */
	public static void transferFilesToSDCard(Resources res) {
		
		Log.e("AAAAAAAAAAAAXXXXX","++++++++++ "+sdCardDir.toString());
		Log.e("AAAAAAAAAAAA","++++++++++ "+sdCardDir.toString());
		Log.e("AAAAAAAAAAAA","++++++++++ "+sdCardDir.toString());
		Log.e("AAAAAAAAAAAA","++++++++++ "+sdCardDir.toString());
		Log.e("AAAAAAAAAAAA","++++++++++ "+sdCardDir.toString());
		Log.e("AAAAAAAAAAAA","++++++++++ "+sdCardDir.toString());
		File andarplusFolder = new File(sdCardDir+"/ARToolKitPlusBenchmark");
		if (!andarplusFolder.exists()) {
			Log.e("AAAAAAAAAAAA","create folder "+andarplusFolder.toString());
			boolean ret = false;
			try{
				ret = andarplusFolder.mkdir();
			}catch(Exception e){
				Log.e("IO", sdCardDir+" not created "+e.getMessage());
			}
			Log.e("IO","ret "+ret);
		}
		if (andarplusFolder.exists()) {
			Log.e("IO","---------TRANSFER FILES TO SD CARD---------------");
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/camera_para.dat",R.raw.camera_para,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/camera_calibration.cal",R.raw.camera_calibration,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.hiro",R.raw.patt_hiro,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.a",R.raw.patt_a,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.b",R.raw.patt_b,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.c",R.raw.patt_c,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.d",R.raw.patt_d,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.f",R.raw.patt_f,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/patt.g",R.raw.patt_g,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/marker.dat",R.raw.marker_dat,res);
			transferFile(sdCardDir+"/ARToolKitPlusBenchmark/markerboard_480_499_cfg.dat",R.raw.markerboard_480_499_cfg,res);
		}else{
			Log.e("AAAAAAAAAAAA","folder does not exists!!!!!!");
		}
	}
	
	static void transferFile(String file,int raw_id,Resources res){
		File tsfFile = new File(file);
		if (!tsfFile.exists()) {
			Log.e("IO","FILE DO NOT EXISTS COPYING....");
			try {
				copy(res.openRawResource(raw_id), new FileOutputStream(tsfFile));
			} catch (NotFoundException e) {
				Log.e("IO","Couldnt transfer file "+file+" into SDCard ",e);
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				Log.e("IO","Couldnt transfer file "+file+" into SDCard ",e);
				e.printStackTrace();
			} catch (IOException e) {
				Log.e("IO","Couldnt transfer file "+file+" into SDCard ",e);
				e.printStackTrace();
			}
		}else{
			Log.e("IO","FILE EXISTS!!!!!");
		}
	}
	
	static void copy( InputStream in, OutputStream out ) throws IOException 
	  { 
	    byte[] buffer = new byte[ 0xFFFF ]; 
	    for ( int len; (len = in.read(buffer)) != -1; ) 
	      out.write( buffer, 0, len ); 
	  }
}